#include <iostream>
#include <memory>
class Bad
{
public:
	std::shared_ptr<Bad> getptr() {
		return std::shared_ptr<Bad>(this);
	}
	~Bad() { std::cout << "Bad::~Bad() called" << std::endl; }
};
 
int main()
{
	// 错误的示例，每个shared_ptr都认为自己是对象仅有的所有者
	std::shared_ptr<Bad> bp1(new Bad());
    // std::shared_ptr<Bad> bp2 = bp1; 
    // 输出结果：
    // bp1.use_count() = 2
    // bp2.use_count() = 2
    // Bad::~Bad() called
	std::shared_ptr<Bad> bp2 = bp1->getptr();
	// 打印bp1和bp2的引用计数
	std::cout << "bp1.use_count() = " << bp1.use_count() << std::endl; // 1
	std::cout << "bp2.use_count() = " << bp2.use_count() << std::endl; // 1
}   // Bad 对象将会被删除两次

    // bp1.use_count() = 1
    // bp2.use_count() = 1
    // Bad::~Bad() called
    // Bad::~Bad() called
    // free(): double free detected in tcache 2
    // Aborted (core dumped)
